package org.liuhy.jbpm4
{
	import flash.events.FocusEvent;
	import flash.events.MouseEvent;
	
	import mx.collections.ArrayCollection;
	import mx.containers.Canvas;
	import mx.containers.VBox;
	
	import org.liuhy.jbpm4.property.TaskProperty;
	import org.liuhy.jbpm4.utils.JbpmRectLineUtils;
	import org.liuhy.line.AbstractLine;
	import org.liuhy.line.CLine;
	import org.liuhy.node.FlowNode;
	import org.liuhy.utils.MyStringUtils;

	public class TaskFlowNode extends FlowNode
	{
		private var _taskType:String;//类型;
		private var _expr:String ;
		private var _property:TaskProperty = null;		
		private var _form:String;		//表单;	
		public function TaskFlowNode(p_x:int,p_y:int,p_width:int,p_height:int)
		{
			super(p_x,p_y,p_width,p_height);	
			_property = new TaskProperty(this);

		}
		
		override public function draw():void{
			this.graphics.clear();
			this.graphics.beginFill(0xFFFFFF,0);
			this.graphics.lineStyle(1,this.borderColor,2);
			this.graphics.drawRect(5,5,this.width-10,this.height-10);
			this.graphics.endFill();
		}
		override public function onFocusChange(event:FocusEvent):void{
			super.onFocusChange(event);
//			_property.taskType = this._taskType; 
		}

		override public function onClick(p_event:MouseEvent):void{
			super.onClick(p_event);
			var _vbox:VBox = this.parent.parent as VBox;
			var _c:Canvas = _vbox.getChildAt(1) as Canvas;
			var _cArray:Array = _c.getChildren();
			for(var i:int=0;i<_cArray.length;i++){
				(_cArray[i] as Canvas).visible = false;
			}
			if(!_c.contains(_property)){
				_c.addChild(_property);
			}else{
//				_property.expr = this._expr;
			}
			_property.visible = true;
			_c.setChildIndex(_property,_c.numChildren-1);			
		}
		override public function toXML():String{
			var _array:ArrayCollection = this._outLineList;
			var _str:String = "<" + this.type + " name=\"" + this.title +  "\" ";
			if("none"!=this._property.taskType && ""!=this.expr){
				_str = _str + this._property._cboxTaskType.selectedItem.code + "=\"" + MyStringUtils.getInstance().encodeXML(this.expr) + "\"";
			}
			if(0!=this._property.form.length){
				_str = _str + " form=\"" + this._property.form + "\" ";
			}
			_str =  _str + " g=\"" + this.x +"," +this.y + "," + this.width + "," + this.height + "\">";

//			if(""==this._expr){
//				_str = "<" + this.type + " name=\"" + this.title + "\" g=\"" + this.x +"," +this.y + "," + this.width + "," + this.height + "\">";
//			}else{
//				_str = "<" + this.type + " name=\"" + this.title + " expr=\"" + MyStringUtils.getInstance().encodeXML(this.expr) + "\" g=\"" + this.x +"," +this.y + "," + this.width + "," + this.height + "\">";
//			}
			for(var i:int=0;i<_array.length;i++){
				var _line:AbstractLine = _array[i] as AbstractLine;
				var _cLine:CLine = _line.parent as CLine;
				var _arrow:FlowNode = _cLine.getArrowFlowNode();
				var _to:String = "";
				var _tran:String = "";
				if(null!=_arrow){
					_to = _arrow.title;
				}
				if(0==JbpmRectLineUtils.getJbpmCLinePosStrs(_cLine).length){
					_tran= "<transition name=\"" + _cLine.title + "\" " + " to=\"" + _to + "\"/>\n";
				}else{
					_tran= "<transition name=\"" + _cLine.title + "\" " + " to=\"" + _to + "\" g=\"" + JbpmRectLineUtils.getJbpmCLinePosStrs(_cLine) + "\"/>\n";
				}
				_str = _str +  _tran ;
			}
			return _str + "</" + this.type + ">";
		}
		override public function get type():String{
			return "task";
		}		
		public function get taskType():String{
			this._taskType =	this._property._cboxTaskType.selectedItem.label;
			return this._property._cboxTaskType.selectedItem.label;
		}
		public function set taskType(p_taskType:String):void{
			this._taskType = p_taskType;
			this._property._cboxTaskType.selectedIndex = 3;
			var _array:ArrayCollection = this._property._taskTypeArray;
			for(var i:int=0;i<_array.length;i++){
				if(_array.getItemAt(i).code==p_taskType){
					this._property._cboxTaskType.selectedIndex = i;
					break;
				}
			}
		}
		public function get form():String{
			return this._form;
		}
		public function set form(p_form:String):void{
			this._form = p_form;
			this._property._txtForm.text = p_form;
		}
		public function get expr():String{
			return this._property._txtExpr.text;
		}
		public function set expr(p_expr:String):void{
			this._property._txtExpr.text = p_expr
			this._expr = p_expr;
		}
	}
}